<template>
	<view class="container"> 
		<view class="content">
			<!-- 如果页面中的cell高度是固定不变的，则不需要设置cell-height-mode，如果页面中高度是动态改变的，则设置cell-height-mode="dynamic" -->
			<!-- 原先的v-model修改为@virtualListChange="virtualListChange"并赋值处理后的虚拟列表 -->
			<z-paging ref="paging" :auto="false" defaultPageSize="20" use-virtual-list :force-close-inner-list="true" :paging-style="{ paddingTop: paddingTop + 'px', paddingBottom: paddingBottom + 'rpx' }" cell-height-mode="dynamic" @virtualListChange="virtualListChange" @query="queryList">
				<!-- 需要固定在顶部不滚动的view放在slot="top"的view中，如果需要跟着滚动，则不要设置slot="top" -->
				<template #top>
					<nav-bar :tabList="tabList" :currentValue="current" @initNav="initNav" @change="change"></nav-bar>
				</template> 
				
				<peiwan @tabClick="tabClick" @search="search" @categoryClick="categoryClick" v-if="tabType == 'peiwan'" :stickyTop="paddingTop" :scrollTop="scrollTop" :virtualList="virtualList"></peiwan>
				<friend @tabClick="tabClick" @search="search" v-if="tabType == 'friend'" :stickyTop="paddingTop" :scrollTop="scrollTop" :virtualList="virtualList"></friend>
			
			</z-paging>
			
			<sex-sheet ref="sexSheet" @sexOk="sexOk"></sex-sheet>
			<su-region-picker :show="cityShow" @cancel="cityShow = false" @confirm="cityOk" />
			<category-sheet ref="categorySheet" @categoryOk="categoryOk"></category-sheet>
		</view>
	</view>
</template>
  
<script>
	import NavBar from '@/pages/tabbar/components/home/navBar.vue';
	import peiwan from '@/pages/tabbar/components/home/peiwan.vue';
	import friend from '@/pages/tabbar/components/home/friend.vue';
	import SexSheet from '@/pages/tabbar/components/home/sexSheet.vue';
	import CategorySheet from '@/pages/tabbar/components/home/categorySheet.vue';
	import sheep from '@/sheep';
	import ClerkApi from '@/sheep/api/worker/clerk';
	import UserApi from '@/sheep/api/member/user';
	import TradeConfigApi from '@/sheep/api/trade/config';
	export default {
		components: {
			NavBar,
			peiwan,
			friend,
			SexSheet,
			CategorySheet,
		},
		props: {
			
		},
		data() {
			return {
				// 虚拟列表数组，通过@virtualListChange监听获得最新数组
				virtualList: [],
				scrollTop: 0,
				paddingTop: 0,
				paddingBottom: 100,
				height: 0,
				
				current: 0,
				
				cityShow: false,
				params: {
					province: true,
					city: true,
					area: false
				},
				
				tabList: [
				{
					name: '达人',
					enabled: false,
					type: 'peiwan',
				},{
					name: '交友',
					enabled: false,
					type: 'friend',
				},],
				
				tabType: '',
				
				queryParams: {
					pageNo: 1,
					pageSize: 20,
					sex: '',
					isCity: 1,
					maxAge: 50,
					minAge: 18,
					keyword: '',
					categoryId: '',
				},
			}
		},
		computed: {
			userInfo: {
			    get() {
			      return sheep.$store('user').userInfo;
			    },
			},
		},
		created() {
			this.getTradeConfig();
		},
		methods: {
			initNav(e) {
				this.height = e.height;
				this.paddingTop = this.height;
			},
			// 监听虚拟列表数组改变并赋值给virtualList进行重新渲染
			virtualListChange(vList) {
				this.virtualList = vList;
			},
			queryList(pageNo, pageSize) {
				// 组件加载时会自动触发此方法，因此默认页面加载时会自动触发，无需手动调用
				// 这里的pageNo和pageSize会自动计算好，直接传给服务器即可
				// 模拟请求服务器获取分页数据，请替换成自己的网络请求
				this.queryParams.pageNo = pageNo;
				this.queryParams.pageSize = pageSize;
				this.queryParams.userId = this.userInfo.id;
				this.getDataPageList(this.queryParams);
			},
			getDataPageList(params) {
				switch(this.tabType){
					case 'peiwan':
						ClerkApi.getClerkPage(params).then(res => {
							this.queryParams.keyword = '';
							// 将请求的结果数组传递给z-paging
							this.$refs.paging.complete(res.data.list);
						}).catch(res => {
							// 如果请求失败写this.$refs.paging.complete(false);
							// 注意，每次都需要在catch中写这句话很麻烦，z-paging提供了方案可以全局统一处理
							// 在底层的网络请求抛出异常时，写uni.$emit('z-paging-error-emit');即可
							this.$refs.paging.complete(false);
						});
					break;
					case 'friend':
						if(this.queryParams.sex == ''){
							params.sex = this.userInfo.sex;
						}
						UserApi.getUserPage(params).then(res => {
							this.queryParams.keyword = '';
							// 将请求的结果数组传递给z-paging
							this.$refs.paging.complete(res.data.list);
						}).catch(res => {
							// 如果请求失败写this.$refs.paging.complete(false);
							// 注意，每次都需要在catch中写这句话很麻烦，z-paging提供了方案可以全局统一处理
							// 在底层的网络请求抛出异常时，写uni.$emit('z-paging-error-emit');即可
							this.$refs.paging.complete(false);
						});
					break;
					case 2:
					break;
				}
			},
			change(e) {
				this.current = e;
				this.tabChange();
				this.scrollTop = 0;
				this.queryParams.sex = "";
				sheep.$store('sys').setSexLabel("性别");
				sheep.$store('sys').setSex2Label("性别");
				this.$refs.paging.reload();
			},
			tabChange() {
				this.tabList = this.tabList.filter(function(tab) {
				    return tab.enabled;
				}); 
				if(this.tabList[this.current]){
					this.tabType = this.tabList[this.current].type;
				}
			}, 
			getTradeConfig() {
				TradeConfigApi.getTradeConfig().then(res => {
					this.tabList[0].enabled = res.data.peiwanEnabled;
					this.tabList[1].enabled = res.data.friendEnabled;
					this.tabChange();
					this.$refs.paging.reload();
				});
			},
			tabClick(e) {
				if('city' == e){
					this.cityShow = true;
				}else if('sex2' == e){ 
					this.$refs.sex2Sheet.openActionSheet();
				}else if('sex' == e){ 
					this.$refs.sexSheet.openActionSheet();
				}else if('category' == e){
					this.$refs.categorySheet.openActionSheet();
				}else if('level' == e){
					
				}
			},
			search(e) {
				this.queryParams.keyword = e;
				this.$refs.paging.reload();
			},
			categoryOk(e) {
				sheep.$store('sys').setCategoryLabel(e.label);
				this.queryParams.categoryId = e.value;
				this.$refs.paging.reload();
			},
			cityOk(e) {
				this.cityShow = false;
				this.queryParams.city = e.city_name;
				sheep.$store('sys').setCityLabel(e.city_name);
				this.$refs.paging.reload();
			},
			categoryClick(e) {
				sheep.$store('sys').setCategoryLabel(e.name);
				this.queryParams.categoryId = e.id;
				this.$refs.paging.reload();
			},
			sexOk(e) {
				sheep.$store('sys').setSexLabel(e.label);
				this.queryParams.sex = e.value;
				this.$refs.paging.reload();
			},
			sex2Ok(e) {
				sheep.$store('sys').setSex2Label(e.label);
				this.queryParams.sex = e.value;
				this.$refs.paging.reload();
			},
			queryUser(e) {
				this.queryParams.sex = e.sex;
				this.queryParams.isCity = e.isCity;
				this.queryParams.maxAge = e.maxAge;
				this.queryParams.minAge = e.minAge;
				this.$refs.paging.reload();
			},
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		background-color: #fafafa;
		height: calc(100vh);
		padding-bottom: env(safe-area-inset-bottom);
	}
	
	
</style>